/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.titan.component.loader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.titan.component.IDirectoryInfo;
import edu.ccut.titan.component.TitanComponentException;
import edu.ccut.titan.component.impl.directory.TitanDirectoryFactory;
import edu.ccut.titan.component.xml.Dom4jUtils;

/**
 * load .titan file for directory information
 * 
 * @author LeslieGu
 * 
 */
public abstract class DirectoryInfoLoader {

	private static final Logger logger = Logger
			.getLogger(DirectoryInfoLoader.class.getPackage().getName());

	private static final String KEY_VERSION = "version";

	private static final String KEY_DESCRIPTION = "description";

	private static final String KEY_TYPE = "type";

	private static final String KEY_TIME = "time";

	public static IDirectoryInfo loadDirectoryInfo(Bundle bundle,
			String filePath) throws TitanComponentException {

		InputStream in = null;
		try {
			URL url = bundle.getEntry(filePath);

			if (url == null) {
				logger.error("can't find file in " + bundle.getSymbolicName()
						+ "with name " + filePath);

				throw new TitanComponentException("can't find file in "
						+ bundle.getSymbolicName() + "with name " + filePath);
			}

			in = url.openStream();

			IDirectoryInfo directoryInfo = loadDirectoryInfo(in);

			String directoryPath = filePath.substring(0, filePath
					.lastIndexOf("/"));

			logger
					.debug("load root directory path = \"" + directoryPath
							+ "\"");
			logger.debug("");

			directoryInfo.setDirectoryPath(directoryPath);

			return directoryInfo;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	private static IDirectoryInfo loadDirectoryInfo(InputStream in)
			throws TitanComponentException {

		Document document = Dom4jUtils.getDocment(in);

		Element root = document.getRootElement();

		String version = root.elementText(KEY_VERSION);
		String description = root.elementText(KEY_DESCRIPTION);
		String type = root.elementText(KEY_TYPE);
		String time = root.elementText(KEY_TIME);

		logger.debug("load root directory version = \"" + version + "\"");
		logger.debug("load root directory description = \"" + description
				+ "\"");
		logger.debug("load root directory type = \"" + type + "\"");
		logger.debug("load root directory time = \"" + time + "\"");

		IDirectoryInfo directoryInfo = TitanDirectoryFactory.getInstance().createDirectory(version, description, type, time);

		return directoryInfo;
	}
}
